<?php
/**
 * 执行点赞入库，需要fabulous_cron_exec.php执行
 */
require_once dirname(dirname(__DIR__)) . "/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\model\UserZan;
use app\lib\db\DbException;
//$process = new Process(__FILE__);
//$process->setProcessNum(5);
//$process->initProcess();
//实例化数据库
$db =  SysResource::getDB(true);
$UserZan = new UserZan($db);
$UserZan->setDb($db);
//实例化redis
$redis = SysResource::getRedis();
try{
    while(true){
        //这里有5个队列,所以循环5次
        for($i=1;$i<=5;$i++){
            //每个队列再去循环100次取数据
            for($j=1;$j<=30;$j++){

                //test为测试，no_test为正式
                $PATH="no_test";
                if($PATH=="test"){
                    $key = REDIS_PREFIX."test_user_material_love_del_".$i;
                    $key1 = REDIS_PREFIX."test_user_material_love_del_1";
                    $key2 = REDIS_PREFIX."test_user_material_love_del_2";
                    $key3 = REDIS_PREFIX."test_user_material_love_del_3";
                    $key4 = REDIS_PREFIX."test_user_material_love_del_4";
                    $key5 = REDIS_PREFIX."test_user_material_love_del_5";
                }else if($PATH=="no_test"){
                    $key = REDIS_PREFIX."user_material_love_del_".$i;
                    $key1 = REDIS_PREFIX."user_material_love_del_1";
                    $key2 = REDIS_PREFIX."user_material_love_del_2";
                    $key3 = REDIS_PREFIX."user_material_love_del_3";
                    $key4 = REDIS_PREFIX."user_material_love_del_4";
                    $key5 = REDIS_PREFIX."user_material_love_del_5";
                }
                //从队列中弹出一个值
                $res=$redis->rPop($key);
                //把对象变为数组
                $data=json_decode($res,true);
                if(!empty($data)){
                    //先去数据库查询该视频喜欢数是否为0
                    $db->select("count_material_love");
                    $db->from("user_material");
                    $db->where(['id'=>$data['id'],'video_uuid'=>$data['video_uuid']]);
                    $query =  $db->get();
                    $datas = $query->one();
        //            var_dump($datas['count_material_love']);exit;
                    //如果喜欢数为0 或者喜欢数为空
                    if($datas['count_material_love']<="0" || $datas['count_material_love']<=0){
                        continue;
                        //修改喜欢数
        //                $re=$db->update("user_material",['count_material_love'=>0],['video_uuid'=>$data['video_uuid'],"id"=>$data['id']]);
                    }else{
                        //修改喜欢数
                        $re=$db->update("user_material",['count_material_love'=>"!count_material_love-1"],['video_uuid'=>$data['video_uuid'],"id"=>$data['id']]);
                    }

                    //添加失败再去重新入队列
                    if(!$re){
                        if($i==1){
                            $redis->lPush($key5,json_encode($data));
                        }else if($i==2){
                            $redis->lPush($key1,json_encode($data));
                        }else if($i==3){
                            $redis->lPush($key2,json_encode($data));
                        }else if($i==4){
                            $redis->lPush($key3,json_encode($data));
                        }else if($i==5){
                            $redis->lPush($key4,json_encode($data));
                        }
                    }
                }else{
                    echo $i;
                    continue;
                }

            }
            sleep(1);
        }
    }
}catch (Exception $e){
    $process->log($e->getMessage());
}
?>